package framework.dao.jdbc.util;

import com.alibaba.druid.pool.DruidDataSource;
import framework.dao.exception.DataAccessException;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

public class JdbcUtil {

    private static Properties config;
    private static DruidDataSource dataSource;
    private static ThreadLocal<Connection> threadLocal;

    static{
        threadLocal=new ThreadLocal<>();
        config=new Properties();
        try{

            config.load(JdbcUtil.class
                                .getClassLoader()
                                .getResourceAsStream("dataSource.properties"));

//            Class.forName(config.getProperty("jdbc.driverClassName"));

            dataSource=new DruidDataSource();
            dataSource.setDriverClassName(config.getProperty("jdbc.driverClassName"));
            dataSource.setUrl(config.getProperty("jdbc.url"));
            dataSource.setUsername(config.getProperty("jdbc.username"));
            dataSource.setPassword(config.getProperty("jdbc.password"));
            dataSource.setMaxActive(Integer.parseInt(config.getProperty("druid.maxActive")));
            dataSource.setInitialSize(Integer.parseInt(config.getProperty("druid.initialSize")));
            dataSource.setMaxWait(Long.parseLong(config.getProperty("druid.maxWait")));


        }catch (Exception e){
            e.printStackTrace();
            throw new ExceptionInInitializerError("JdbcUtil初始化失败");
        }
    }


    public static Connection getConnection(){
        Connection con=null;

        try{
            con=threadLocal.get();
            if(con==null) {
//            con= DriverManager.getConnection(config.getProperty("jdbc.url"),
//                                             config.getProperty("jdbc.username"),
//                                             config.getProperty("jdbc.password"));

                con = dataSource.getConnection();
                threadLocal.set(con);
            }

        }catch (Exception e){
            e.printStackTrace();
            throw new DataAccessException("数据访问失败",e);
        }


        return con;
    }

    public static void close(){
        Connection con=null;
        try{
            con=threadLocal.get();
            if(con!=null){
                con.close();
                threadLocal.remove();
            }


        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void close(ResultSet rs, PreparedStatement ps,Connection con){
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(ps!=null){
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(con!=null){
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }


    public static void main(String[] args) {
        System.out.println(getConnection());
        System.out.println("正确获得连接后的代码");
    }


}
